from . import db
from sqlalchemy.types import JSON
from uuid import uuid4
from sqlalchemy import func, DateTime
from sqlalchemy.dialects.mysql import BIGINT

class Elderdemand(db.Model):
    __tablename__ = 'elder_demand'
    id = db.Column(db.BigInteger, primary_key=True)
    elder = db.Column(db.String(255), nullable=False)
    actions = db.Column(db.String(255), nullable=False)
    arrange = db.Column(db.String(1), default='0')  # 新增字段，默认值为'0'

    def __repr__(self):
        return f'<Elder {self.id} {self.elder} arrange:{self.arrange}>'

class Attendantskills(db.Model):
    __tablename__ = 'attendant_skills'
    id = db.Column(db.String(255), primary_key=True, default=lambda: str(uuid4()))
    attendant_name = db.Column(db.String(255), nullable=False)
    skills = db.Column(db.String(255), nullable=False)

class CaregiverSchedule(db.Model):
    __tablename__ = 'caregiver_schedules'
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid4()))
    caregiver_id = db.Column(db.String(36), db.ForeignKey('attendant_skills.id'), nullable=False)
    schedule_date = db.Column(db.DateTime, nullable=False)
    time_slot = db.Column(db.Integer, nullable=False)
    is_available = db.Column(db.Boolean, default=True)

    def __repr__(self):
        return f'<CaregiverSchedule {self.caregiver_id} {self.schedule_date} {self.time_slot}>'

class DicNursingAction(db.Model):
    __tablename__ = 'dic_nursing_actions'
    id = db.Column(db.BigInteger, primary_key=True)
    pid = db.Column(db.BigInteger)
    name = db.Column(db.String(255), nullable=False)
    level = db.Column(db.String(255))
    nursing_level = db.Column(db.String(255))

    def __repr__(self):
        return f'<DicNursingAction {self.id} {self.name}>'

class ScheduleResult(db.Model):
    __tablename__ = 'schedule_results'
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid4()))
    elder = db.Column(db.String(36), nullable=False)
    schedule_date = db.Column(DateTime, nullable=False)
    schedule_data = db.Column(JSON, nullable=False)

    def __repr__(self):
        return f'<ScheduleResult {self.id} {self.elder} {self.schedule_date}>'

class TimeSlot(db.Model):
    __tablename__ = 'time_slots'
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid4()))
    schedule_result_id = db.Column(db.String(36), db.ForeignKey('schedule_results.id'), nullable=False)
    elder = db.Column(db.String(36), nullable=False)
    time_slot = db.Column(db.String(11), nullable=False)
    activity = db.Column(db.String(100), nullable=False)
    activity_id = db.Column(db.String(100), nullable=False)
    caregiver = db.Column(db.String(100), nullable=False)
    caregiver_id = db.Column(db.String(100), nullable=False)
    time_order = db.Column(db.Integer, nullable=False)
    schedule_date = db.Column(DateTime, nullable=False)

    def __repr__(self):
        return f'<TimeSlot {self.id} {self.elder} {self.time_slot} {self.activity}>'

    @classmethod
    def get_ordered_slots(cls):
        return cls.query.order_by(
            cls.schedule_result_id,
            cls.elder,
            cls.schedule_date,
            func.substr(cls.time_slot, 1, 2),
            func.substr(cls.time_slot, 4, 2)
        ).all()